/**
 * 
 */
package com.cloud.zuul.inteceptor;

import java.net.URLEncoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import com.cloud.common.CommonConstants;

/**
 *
 */
@Component
public class AuthActionInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		Cookie cookie = WebUtils.getCookie(request, CommonConstants.TOKEN_COOKIE);
		String requestURI = request.getRequestURI();
		System.out.println(requestURI);
		if (cookie != null && StringUtils.isNoneBlank(cookie.getValue())) {
			return true;
		} else {
			String msg =  URLEncoder.encode("请先登录", "utf-8");
			response.sendRedirect("/login?target=" + msg);
			return false;
		}
	}

	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse res, Object handler, ModelAndView modelAndView)
			throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest req, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
	
}
